﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Reflection;

namespace QL_Truong_Hoc.HocTap
{
    public partial class frmThiHetMonNghe : DevExpress.XtraEditors.XtraForm
    {
        private int mintKdtID;
        private int mintLopID;

        public bool Changed { get; set; }

        public frmThiHetMonNghe()
        {
            InitializeComponent();
        }

        private void frmThiHetMonNghe_Load(object sender, EventArgs e)
        {
            //// TODO: This line of code loads data into the 'qLTHDataSet.mKhungDaoTaoMon_GetAllWithDetail' table. You can move, or remove it, as needed.
            this.mKhungDaoTaoMon_GetAllWithDetailTableAdapter.Fill(this.qLTHDataSet.mKhungDaoTaoMon_GetAllWithDetail);
            this.mKhungDaoTaoMonGetAllWithDetailBindingSource.Filter = string.Format("KDT_ID = {0}", clsConst.gcintNoneValue);

            //fill năm tuyển sinh
            for (int i = 0; i <= 5; i++)
            {
                cbNam.Properties.Items.Add(DateTime.Now.Year + 1 - i);
            }

            //fill lần thi
            cbLan.Properties.Items.Add("Lần 1");
            cbLan.Properties.Items.Add("Lần 2");
            cbLan.SelectedIndex = 0;

            tabControl.Focus();
            cbNam.Focus();
            this.Changed = false;
        }

        private void btnChonLop_Click(object sender, EventArgs e)
        {
            try
            {
                using (frmChonLopNghe frmChonLop = new frmChonLopNghe())
                {
                    frmChonLop.ShowDialog();
                    if (!frmChonLop.Selected) return;
                    this.mintKdtID = frmChonLop.KhungDaoTaoID;
                    this.mintLopID = frmChonLop.LopID;
                    this.txtLop.Text = frmChonLop.LopTen;

                    //fill lại môn
                    cbMon.Properties.AllowNullInput = DevExpress.Utils.DefaultBoolean.True;
                    cbMon.EditValue = null;

                    this.mKhungDaoTaoMonGetAllWithDetailBindingSource.Filter = string.Format("KDT_ID = {0}", mintKdtID);
                }
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (!validControl.Validate()) return;

                int intNam;
                int intLan;
                bool blnSuccess;

                int.TryParse(cbNam.Text, out intNam);
                intLan = cbLan.SelectedIndex;

                DTO.MThiHetMonDTO objThi = new DTO.MThiHetMonDTO();
                objThi.KDT_ID = mintKdtID;
                objThi.Lop_ID = mintLopID;
                objThi.Thm_nam = intNam;
                objThi.LanThi_ID = intLan;
                objThi.MO_ID = clsCommon.fncCnvNullToInt(cbMon.EditValue);

                try
                {
                    blnSuccess = DAO.MThiHetMonDAO.Insert(Program.gobjDb, Program.gobjTrans, objThi);
                    if (!blnSuccess)
                    {
                        clsCommon.fncMsgError("Lưu thông tin thất bại, xin hãy thử lại.");
                        cbNam.Focus();
                        return;
                    }
                    else
                    {
                        this.Changed = true;
                        this.Close();
                    }
                }
                catch
                {
                    clsCommon.fncMsgError("Lưu thông tin thất bại, xin hãy thử lại.");
                    cbNam.Focus();
                    return;
                }
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            try
            {
                if (!clsCommon.fncMsgConfirm("Bạn có chắc muốn thoát?")) return;
                this.Close();
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }

    }
}